<?php
/*****************************
* PHPwork官方网址： ruanjiesoft.com
* PHPwork应用官方QQ群： 317075601
* PHPwork应用开发者QQ： 1980572700
* 不得使用本软件用于非法用途，不得违反国家法律。
*************************************/

namespace application\cms\controller;

require 'base.php';

class Article extends Base{

    //文章
    function index(){   
        
    } 

    //文章分类 - 文章列表
    function article_list(){   
        $sort_id = intval($_GET['sort_id']);
        
        $sql="select * from ".tablename('rj_cms_article_sort')." where status=1 and sort_id=$sort_id ";
        $article_sort=$GLOBALS['db']->query($sql)->fetch();
        
        $sql="select * from ".tablename('rj_cms_article')." where status=1 and sort_id=$sort_id order by update_time desc,id desc ";
        $page= \library\deep\Page::new_instance();   // 分页
        $sql=$page->getPageSQL( $sql , 20 );
        $article=$GLOBALS['db']->query($sql);

		$this->config['title'] = $article_sort['sort_name']; // 网页标题

		require view('default/' . $article_sort['tpl_name'] ); //article_list.tpl.php
    } 

    //文章详细
    function article_detail(){   
        
        if(isset($_GET['id']) && $_GET['id']!=''){
            $id = intval( $_GET['id'] );
			// 更新浏览量
            $sql="update ".tablename('rj_cms_article')." set hits=hits+1,hits_last_time= ".time()." where status=1  and id= $id ";
            $GLOBALS['db']->exec($sql);

            $sql="select * from ".tablename('rj_cms_article')." where status=1  and id= $id ";
            $article=$GLOBALS['db']->query($sql)->fetch();
			
            // 文章对应的分类
            $sql="select * from ".tablename('rj_cms_article_sort')." where status=1 and sort_id= " . $article['sort_id'] ;
            $article_sort=$GLOBALS['db']->query($sql)->fetch();  

            //前一篇 上一篇 时间戳排序方式倒叙 1000020003 1000020002 1000020001
            $sql="select * from ".tablename('rj_cms_article')." where status=1 and sort_id={$article['sort_id']} and update_time > {$article['update_time']} ORDER BY update_time,id DESC LIMIT 0,1 ";
            $article_up=$GLOBALS['db']->query($sql)->fetch();

            //下一篇
            $sql="select * from ".tablename('rj_cms_article')." where status=1  and sort_id={$article['sort_id']}  and update_time < {$article['update_time']} ORDER BY update_time DESC,id LIMIT 0,1 ";
            $article_down=$GLOBALS['db']->query($sql)->fetch();
			
			$this->config['title'] = $article['title']; // 网页标题

        }else{
            header('Location:'.url([ _MODULE_NAME_ ,'index','index']));
            exit;
        }

		require view('default/' . $article['tpl_name'] ); //article_detail.tpl.php

    } 


}
